package com.jsyh.maidu.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * TimeUtils
 * 
 *
 */
public class TimeUtils {

    public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat DATE_FORMAT_DATE    = new SimpleDateFormat("yyyy-MM-dd");


    public static final int DAY_SECOND = 86400;
    public static final int HOUR_SECOND = 3600;
    public static final int MINUTE_SECOND = 60;


    public static String setTime(long time) {
        StringBuilder sb = new StringBuilder();
        if (time >= DAY_SECOND) {
            long day = time / DAY_SECOND;
            long dcount = time % DAY_SECOND;
            long hout = dcount / HOUR_SECOND;
            long hcount = dcount % HOUR_SECOND;
            long min = hcount / MINUTE_SECOND;
            long mcount = hcount % MINUTE_SECOND;
            sb.append(day).append(":").append(hout).append(":").append(min).append(":").append(mcount);
            return sb.toString();
        } else if (time < DAY_SECOND && time >= HOUR_SECOND) {
            long hout = time / HOUR_SECOND;
            long hcount = time % HOUR_SECOND;
            long min = hcount / MINUTE_SECOND;
            long mcount = hcount % MINUTE_SECOND;
            sb.append(hout).append(":").append(min).append(":").append(mcount);
            System.out.println(sb.toString());
        } else if (time < HOUR_SECOND && time >= MINUTE_SECOND) {
            long min = time / MINUTE_SECOND;
            long mcount = time % MINUTE_SECOND;
            sb.append(min).append(":").append(mcount);
            System.out.println(sb.toString());
        } else if (time > 0) {
            sb.append(time);
        } else {
            sb.append("00:00:00");
        }
        return sb.toString();
    }

    private TimeUtils() {
        throw new AssertionError();
    }

    /**
     * long time to string
     * 
     * @param timeInMillis
     * @param dateFormat
     * @return
     */
    public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
        return dateFormat.format(new Date(timeInMillis));
    }

    /**
     * long time to string, format is {@link #DEFAULT_DATE_FORMAT}
     * 
     * @param timeInMillis
     * @return
     */
    public static String getTime(long timeInMillis) {
        return getTime(timeInMillis, DEFAULT_DATE_FORMAT);
    }

    /**
     * get current time in milliseconds
     * 
     * @return
     */
    public static long getCurrentTimeInLong() {
        return System.currentTimeMillis();
    }

    /**
     * get current time in milliseconds, format is {@link #DEFAULT_DATE_FORMAT}
     * 
     * @return
     */
    public static String getCurrentTimeInString() {
        return getTime(getCurrentTimeInLong());
    }

    /**
     * get current time in milliseconds
     * 
     * @return
     */
    public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
        return getTime(getCurrentTimeInLong(), dateFormat);
    }
}
